package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.stadium.api.req.*;
import com.quanyan.stadium.service.gameSign.GameSignUpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zyq
 * Date: 2016/6/27 0027
 * Time: 16:36
 */
@RestController
@RequestMapping("/api/stadium/gameSignUp")
public class GameSignUpController {

    @Autowired
    GameSignUpService gameSignUpService;

    private final static Logger logger = LoggerFactory.getLogger(GameSignUpController.class);

    /**
     * 查询用户所加入的俱乐部，以及是否有参赛资格
     * @param request
     * @param servletRequest
     * @return
     */
    @RequestMapping(value="/getSignClub", method = RequestMethod.POST)
    public APIResponse<?> getSignClub(@RequestBody ReqUserSignUp request,HttpServletRequest servletRequest) {
        request.setUid((Integer) servletRequest.getAttribute("uid"));
        return gameSignUpService.getSignClub(request);
    }

    @RequestMapping(value="/getUserOrder", method = RequestMethod.POST)
    public APIResponse<?> getUserOrder(@RequestBody ReqUserOrder request,HttpServletRequest servletRequest) {
        request.setUid((Integer) servletRequest.getAttribute("uid"));
        return gameSignUpService.queryUserOrder(request);
    }




    @RequestMapping(value="/userSign", method = RequestMethod.POST)
    public APIResponse<?> userSign(@RequestBody ReqUserSignUp request,HttpServletRequest servletRequest) {
        request.setUid((Integer) servletRequest.getAttribute("uid"));
        return gameSignUpService.userSign(request);
    }



    @RequestMapping(value="/gameOrder", method = RequestMethod.POST)
    public APIResponse<?> gameOrder(@Valid @RequestBody ReqPay request,HttpServletRequest servletRequest) {
        request.setUid((Integer) servletRequest.getAttribute("uid"));
        request.setVer(servletRequest.getHeader("APPVersion"));
        boolean isHelpSign = false;
        if(request.getIsHelpSign() == 1){
            isHelpSign = true;
        }
        return gameSignUpService.gameOrder(request,isHelpSign);
    }

    @RequestMapping(value="/verifyCode", method = RequestMethod.POST)
    public APIResponse<?> verifyCode(HttpServletRequest servletRequest) {
        Integer uid = (Integer) servletRequest.getAttribute("uid");
        return APIResponse.returnSuccess(gameSignUpService.sendSMS(uid));
    }


    @RequestMapping(value="/abstainGame", method = RequestMethod.POST)
    public APIResponse<?> abstainGame(@Valid @RequestBody ReqAbstain request,HttpServletRequest servletRequest) {
        Integer uid = (Integer) servletRequest.getAttribute("uid");
        request.setUid(uid);
        return APIResponse.returnSuccess(gameSignUpService.abstain(request));
    }

    @RequestMapping(value="/outerSignUp", method = RequestMethod.POST)
    public APIResponse<?> outerSignUp(@Valid @RequestBody ReqOuterSign reqOuterSign, HttpServletRequest request) {
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == 0){
            return APIResponse.returnFail("报名失败！");
        }
        List<Integer> uids = new ArrayList<>();
        uids.add(uid);
        Map<Integer,Integer> map = gameSignUpService.outerGameSignUp(uids,reqOuterSign.getGameId(), true);
        if(map == null || map.size() == 0){
            return APIResponse.returnFail("报名失败！");
        }
        return APIResponse.returnSuccess(map);
    }

    @RequestMapping(value="/checkUser", method = RequestMethod.POST)
    public APIResponse<?> checkUser(@Valid @RequestBody ReqCheckUser reqCheckUser, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(bindingResult.getFieldErrors().toString());
        }
        return gameSignUpService.checkUser(reqCheckUser);
    }

    @RequestMapping(value="/recentHelpSignUser", method = RequestMethod.POST)
    public APIResponse<?> recentHelpSignUser(@Valid @RequestBody ReqRecentSginUser request, BindingResult bindingResult,HttpServletRequest servletRequest) {
        request.setUid((Integer) servletRequest.getAttribute("uid"));
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(bindingResult.getFieldErrors().toString());
        }
        return APIResponse.returnSuccess(gameSignUpService.getRecentSignUser(request));
    }

    @RequestMapping(value="/helpSign", method = RequestMethod.POST)
    public APIResponse<?> helpSign(@Valid @RequestBody ReqRecentSginUser request, BindingResult bindingResult,HttpServletRequest servletRequest) {
        Integer uid = (Integer)servletRequest.getAttribute("uid");
        request.setUid(uid);
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(bindingResult.getFieldErrors().toString());
        }
        return gameSignUpService.helpSign(request);
    }

    /**
     * 查询俱乐部成员
     * @param request
     * @param bindingResult
     * @param servletRequest
     * @return
     */
    @RequestMapping(value="/getClubUser", method = RequestMethod.POST)
    public APIResponse<?> getClubUser(@Valid @RequestBody ReqRecentSginUser request, BindingResult bindingResult,HttpServletRequest servletRequest) {
        Integer uid = (Integer)servletRequest.getAttribute("uid");
        request.setUid(uid);
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(bindingResult.getFieldErrors().toString());
        }
        return gameSignUpService.getClubUser(request);
    }

    /**
     * 查询用户信息
     * @param request
     * @param bindingResult
     * @param servletRequest
     * @return
     */
    @RequestMapping(value="/getUserMsg", method = RequestMethod.POST)
    public APIResponse<?> getUserMsg(@Valid @RequestBody ReqUserMsg request, BindingResult bindingResult,HttpServletRequest servletRequest) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(bindingResult.getFieldErrors().toString());
        }
        return gameSignUpService.getUserMsg(request);
    }
}
